package com.nuc.bigdata.exception;

import com.alibaba.fastjson.JSON;
import com.nuc.bigdata.model.ResultCode;

import java.util.HashMap;

/**
 * 自定义业务异常类
 * @author JackLuo
 * @date 2019/10/31
 */
public final class BusinessException extends RuntimeException{

    private static final long serialVersionUID = 2332608236621015980L;

    public static final String CLASS_CODE = "pnDSoTCLYRqeHEVJU6jv/A==";
    public static final String CLASS_MSG = "CLASS";

    /**
     * 异常编码
     */
    private Integer code;

    /**
     * 异常摘要
     */
    private String msg;

    /**
     * 异常对象
     */
    private Object error;

    public BusinessException(Integer code,String msg,Object error) {
        this.code=code;
        this.msg = msg;
        this.error = error;
    }

    public BusinessException(ResultCode resultCode,Object error) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.error = error;
    }

    @Override
    public String getMessage() {
        return generateJson();
    }


    public String generateJson() {
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", getCode());
        map.put("msg",getMsg() );
        map.put("error",getError() );
        map.put(CLASS_MSG,CLASS_CODE );
        return JSON.toJSONString(map);
    }

    @Override
    public String toString() {
        return "BusinessException{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", error=" + error +
                '}';
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getError() {
        return error;
    }

    public void setError(Object error) {
        this.error = error;
    }
}
